package com.cj.controller;

import com.cj.po.Dormclean;
import com.cj.po.PageInfo;
import com.cj.service.DormcleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 班级控制器类
 */
@Controller
public class DormcleanControlleer {
    // 依赖注入
    @Autowired
    private DormcleanService dormcleanService; // 修改为小写 DormcleanService

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findDormClean")
    public String findDormClean(Integer d_id, String d_dormbuilding, Integer pageIndex, Integer pageSize, Model model) {

        // 获取分页信息
        PageInfo<Dormclean> pi = dormcleanService.findPageInfo(d_id, d_dormbuilding, pageIndex,pageSize);
        model.addAttribute("di", pi);
        return "dormclean_list"; // 返回视图
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportdormcleanlist", method = RequestMethod.POST)
    @ResponseBody
    public List<Dormclean> exportdormclean() {
        // 获取所有班级信息
        List<Dormclean> dormcleanList = dormcleanService.getAll();
        return dormcleanList;
    }
    /**
     * 删除宿舍信息
     */
    @RequestMapping("/deleteDormClean")
    @ResponseBody
    public String deleteDormclean(Integer g_id) { // 修改参数为
        int result = dormcleanService.deleteDormclean(g_id); // 删除
        return "dormclean_list"; // 返回宿舍列表视图
    }

    /**
     * 添加宿舍信息
     */
    @RequestMapping(value = "/addDormClean", method = RequestMethod.POST)
    @ResponseBody
    public String addDormclean(@RequestBody Dormclean dormclean) { // 修改为 Dormclean
        int result = dormcleanService.addDormclean(dormclean); // 添加宿舍
        return "dormclean_list"; // 返回宿舍列表视图
    }
    /**
     * 修改宿舍信息
     */
    @RequestMapping(value = "/updateDormClean", method = RequestMethod.POST)
    public String updateDormclean(Dormclean dormclean) { // 修改为 DClass
        int result = dormcleanService.updateDormclean(dormclean); // 更新宿舍信息
        return "redirect:/findDormClean"; // 跳转到宿舍列表
    }

    /**
     * 根据班级ID查找宿舍信息
     */
    @RequestMapping("/findDormCleanById")
    public String findDormCleanById(Integer g_id, HttpSession session) { // 修改为 d_id
        Dormclean dormclean = dormcleanService.findDormCleanById(g_id); // 查找宿舍信息
        session.setAttribute("d", dormclean); // 将宿舍息存入会话
        return "dormclean_edit"; // 返回宿舍编辑页面
    }
}
